﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace NCAABasketballTournament
{
	public partial class BracketPage : UserControl
	{
        Tournament oTournament = null;


        public BracketPage()
		{
			// Required to initialize variables
			InitializeComponent();

            //MarkTextBoxWithName();
            ClearAllTextBlocks();
		}

        private void btnCalculate_Click(object sender, RoutedEventArgs e)
        {
            oTournament = DataContext as Tournament;

            FillRound1(oTournament.MidWest);
            FillRound1(oTournament.East);
            FillRound1(oTournament.South);
            FillRound1(oTournament.West);

            PlayRound1.Completed += new EventHandler(PlayRound1_Completed);
            PlayRound1.Begin();
        }

        void PlayRound1_Completed(object sender, EventArgs e)
        {
            FillRound2(oTournament.MidWest);
            FillRound2(oTournament.East);
            FillRound2(oTournament.South);
            FillRound2(oTournament.West);

            PlayRound2.Completed += new EventHandler(PlayRound2_Completed);
            PlayRound2.Begin();
        }

        void PlayRound2_Completed(object sender, EventArgs e)
        {
            FillRound3(oTournament.MidWest);
            FillRound3(oTournament.East);
            FillRound3(oTournament.South);
            FillRound3(oTournament.West);
            PlayRound3.Completed += new EventHandler(PlayRound3_Completed);
            PlayRound3.Begin();
        }

        void PlayRound3_Completed(object sender, EventArgs e)
        {
            FillRound4(oTournament.MidWest);
            FillRound4(oTournament.East);
            FillRound4(oTournament.South);
            FillRound4(oTournament.West);
            PlayRound4.Completed += new EventHandler(PlayRound4_Completed);
            PlayRound4.Begin();
        }

        void PlayRound4_Completed(object sender, EventArgs e)
        {
            FillRound5(oTournament.MidWest);
            FillRound5(oTournament.East);
            FillRound5(oTournament.South);
            FillRound5(oTournament.West);
            PlayRound5.Completed += new EventHandler(PlayRound5_Completed);
            PlayRound5.Begin();
        }

        void PlayRound5_Completed(object sender, EventArgs e)
        {
            FillRound2(oTournament.Finals);
            PlayRound6.Completed += new EventHandler(PlayRound6_Completed);
            PlayRound6.Begin();
        }

        void PlayRound6_Completed(object sender, EventArgs e)
        {
            FillRound3(oTournament.Finals);
            PlayRound7.Begin(); 
        }

        private void ClearAllTextBlocks()
        {
            foreach (var oItem in LayoutRoot.Children)
            {
                TextBlock oBlock = oItem as TextBlock;
                if (oBlock == null)
                    continue;

                oBlock.Text = "";

                //string sName = oBlock.Name;
                //if (sName.StartsWith("M") || sName.StartsWith("W") || sName.StartsWith("F"))
                //    oBlock.TextAlignment = TextAlignment.Left;
                //else
                //    oBlock.TextAlignment = TextAlignment.Right;

                ////oBlock.Text = "";
                //oBlock.Width += 30.0;
            }
        }
        private void MarkTextBoxWithName()
        {
            foreach (var oItem in LayoutRoot.Children)
            {
                TextBlock oBlock = oItem as TextBlock;
                if (oBlock == null)
                    continue;

                string sName = oBlock.Name;
                oBlock.Text = sName;
            }
        }

        private void PushValue(string sRegion, int iRound, int iSlot, string sValue)
        {
            try
            {
                string sKey = string.Format("{0}R{1}S{2}", sRegion, iRound, iSlot);
                TextBlock oBlock = LayoutRoot.FindName(sKey) as TextBlock;
                oBlock.Text = sValue;
            }
            catch { }
        }

        private void FillRound1(Bracket oBracket)
        {
            string sRegion = oBracket.Name.Substring(0, 1);

            for (int i = 0; i < oBracket.Round1.Count; i++)
                PushValue(sRegion, 1, i + 1, oBracket.Round1[i].TeamName);
        }

        private void FillRound2(Bracket oBracket)
        {
            string sRegion = oBracket.Name.Substring(0, 1);

            for (int i = 0; i < oBracket.Round2.Count; i++)
                PushValue(sRegion, 2, i + 1, oBracket.Round2[i].TeamName);
        }

        private void FillRound3(Bracket oBracket)
        {
            string sRegion = oBracket.Name.Substring(0, 1);

            for (int i = 0; i < oBracket.Round3.Count; i++)
                PushValue(sRegion, 3, i + 1, oBracket.Round3[i].TeamName);
        }

        private void FillRound4(Bracket oBracket)
        {
            string sRegion = oBracket.Name.Substring(0, 1);

            for (int i = 0; i < oBracket.Round4.Count; i++)
                PushValue(sRegion, 4, i + 1, oBracket.Round4[i].TeamName);
        }


        private void FillRound5(Bracket oBracket)
        {
            string sRegion = oBracket.Name.Substring(0, 1);

            for (int i = 0; i < oBracket.Round5.Count; i++)
                PushValue(sRegion, 5, i + 1, oBracket.Round5[i].TeamName);
        }

	}
}